<template>
  <div>
    <div style=" text-align: center;">
      <h1>新闻管理</h1>
    </div>
    <!-- 新闻列表 -->
    <div v-for="(item,index) in NewsList" :key="index">
      <el-descriptions title="新闻信息" :column="1" class="custom-border">
        <el-descriptions-item label="新闻名称">{{item.title}}</el-descriptions-item>
        <el-descriptions-item label="新闻内容">
          <!-- {{item.content  }} -->
          <p v-html="item.content" />
        </el-descriptions-item>
        <template slot="extra">
          <el-button type="primary" class="btn" size="small" @click="dialogForm(item.id)">查看</el-button>
          <el-button type="primary" class="btn" size="small" @click="showDeleteConfirm(item.id,item.title)">删除</el-button>
        </template>
      </el-descriptions>
    </div>
    <!-- 弹窗表单 -->
    <el-dialog title="新闻详情" :visible.sync="dialogFormVisible" center>
      <el-form :model="NewsForm" ref="NewsForm" label-width="80px" :rules="rules">
        <el-form-item label="新闻名称" prop="title">
          <el-input v-model="NewsForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新闻内容" prop="content">
          <Editor :height="200" v-model="NewsForm.content"></Editor>
        </el-form-item>
        <!-- <el-form-item label="新闻状态">
          <el-radio-group v-model="NewsForm.projectStatus">
            <el-radio :label="'0'">进行中</el-radio>
            <el-radio :label="'1'">已完成</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="reviseForm(NewsForm)">确定修改</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[1, 2, 3, 4, 5]" :current-page="queryParams.pageNum" :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" />
    </div>

  </div>
</template>
<script>
import { JigouNewsList, JigouNesById, reviseNews, deleteNews } from '@/api/api'
export default {
  data() {
    return {
      // 表单弹窗开关
      dialogFormVisible: false,
      // 新闻列表
      NewsList: [],
      // 新闻表单
      NewsForm: {
        projectStatus: '0'
      },
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 3
      },
      gerenlist: {},
      id: '',
      // 图片上传
      fileList: [],
      // 表单效验
      rules: {
        title: [{ required: true, message: '请输入新闻名称', trigger: 'blur' }],
        projectDescribe: [{ required: true, message: '请输入新闻描述', trigger: 'blur' }],
        content: [{ required: true, message: '请输入新闻内容', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 查看按钮
    dialogForm(id) {
      this.dialogFormVisible = true
      this.id = id
      // console.log(this.NewsForm.id)

      JigouNesById(id).then(res => {
        this.NewsForm = res.data.data
        // console.log(this.NewsForm)
        // console.log(this.NewsForm)
      })
    },
    // 询问是否删除
    showDeleteConfirm(id, title) {
      this.$confirm(`此操作将永久删除《${title}》, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delNewsList(id)
        })
        .catch(() => {
          this.$message({
            type: 'warning',
            message: '已取消删除'
          })
        })
    },
    // // 删除按钮
    delNewsList(id) {
      deleteNews(id)
        .then(res => {
          this.$message.success('删除成功')
          this.getList()
          console.log(res)
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 新闻列表
    getList() {
      const queryParams = {
        id: this.gerenlist.id,
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize
      }
      JigouNewsList(queryParams).then(res => {
        this.NewsList = res.data.data.list
        this.total = parseInt(res.data.data.total)
        // console.log('this.NewsList', res)
      })
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },
    // 表单修改
    reviseForm(NewsForm) {
      this.$refs.NewsForm.validate(valid => {
        //开启校验
        if (valid) {
          // 如果校验通过，请求接口，允许提交表单
          this.dialogFormVisible = false
          const NewsFormlist = {
            id: this.id,
            title: NewsForm.title,
            content: NewsForm.content
          }
          reviseNews(NewsFormlist).then(result => {
            if (result.data.code == 200) {
              this.getList()
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            } else {
              this.$message.error({
                message: '修改失败' + result.data.message,
                type: 'error'
              })
            }
          })
        } else {
          //校验不通过
          return false
        }
      })
    }
  },
  mounted() {
    this.gerenlist = JSON.parse(localStorage.getItem('list'))
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.pagination-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px; /* 根据需要设置高度 */
}
.custom-border {
  border-bottom: 1px solid #000000; /* 边框颜色和宽度可以根据需要调整 */
  border-radius: 4px; /* 设置边框圆角 */
  margin-bottom: 25px;
}
p {
  width: 555px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}
// ::v-deep tbody {
//   height: 50px;
// }
::v-deep .el-descriptions__body {
  height: 65px;
}
::v-deep .el-descriptions__header {
  height: 25px;
}
:deep .el-descriptions__extra {
  position: relative;
  top: 50px;
}
:deep .el-descriptions__extra .btn {
  display: flex;
  // justify-content: center;
  // align-items: center;
  margin-left: auto;
  margin-top: 10px;
}
</style>